#-------------------------------------------------------------------------------
#    File      : Makefile
#    This file is compiling 8051 project.
#
#    Change Logs:
#    Date           Author       Notes
#    2022-05-05     LinuxLife    the first version
#    2022-05-06     LinuxLife    add print successful info
#    2023-12-31     LinuxLife    repair memory mode error
#    2024-12-23     LinuxLife    add stcgal option
#    2025-07-31     LinuxLife    remove lib and sdld config
#    2025-08-03     LinuxLife    add mcu code and ram option
#                                add shell select option
#    2025-10-24     LinuxLife    remove shell select option
#                                remove asm options
#                                change memory info mode
#                                change "get object file"
#                                change lib options
#                                change log output
#                                change stcgal serial option
#                                add eeprom_image option
#                                add search serial port device
#    2025-10-25     LinuxLife    change comments ,layout and options
#                                change command clean
#-------------------------------------------------------------------------------




#-------------------------------------------------------------------------------
#	Project option
#
#   target       : project name
#   archive      : mcu archive
#   eeprom_image : eeprom image file
#   serial       : COMx(windows) or /tty/USBx(Linux) ,and its options
#   rom_base     : inside rom address base
#   rom_size     : inside rom size
#   xram_base    : outside ram address base
#   xram_size    : outside ram size
#   iram_size    : inside ram size ,default are 0x0080 and 0x0100
#   stack_base   : stack pointer initial value ,default is 0x07
#   xstack_base  : outside stack start address ,default is null
#   stack_size   : default is null (auto)
#-------------------------------------------------------------------------------
target       := stc89
archive      := mcs51
eeprom_image := 
serial       := 
rom_base     := 0x0000
rom_size     := 0x2000
xram_base    := 0x0100
xram_size    := 0x0100
iram_size    := 0x0100
stack_base   := 0x07
xstack_base  := 
stack_size   := 


#
#   Enter your source path
#
INCDIRS :=  arch \
            bsp \
			module \
			src

SRCDIRS :=  arch \
            bsp \
			module \
			src


# For example :
# LIBS := module/test/example.lib
LIBS := 


#-------------------------------------------------------------------------------
#	Toolchain option
#-------------------------------------------------------------------------------
CC := sdcc


CC_CONFIG   := -m$(archive) --model-medium
LD_CONFIG   := --code-loc $(rom_base) \
			   --code-size $(rom_size) \
               --xram-loc $(xram_base) \
			   --xram-size $(xram_size) \
               --iram-size $(iram_size)
#			   --stack-loc $(stack_base)
#			   --stack-size $(stack_size)
#			   --xstack-loc $(xstack_base)
			   


#-------------------------------------------------------------------------------
#	get source file
#   do not edit them
#-------------------------------------------------------------------------------
INCLUDE	:= $(patsubst %, -I %, $(INCDIRS))

#	get file
CFILES		:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))

#	get file path
CFILENDIR	:= $(notdir  $(CFILES))

#	get object file
OBJS_REL	:= $(patsubst %, %, $(CFILENDIR:.c=.rel))
OBJS_ASM	:= $(patsubst %, %, $(CFILENDIR:.c=.asm))
OBJS_LST	:= $(patsubst %, %, $(CFILENDIR:.c=.lst))
OBJS_RST	:= $(patsubst %, %, $(CFILENDIR:.c=.rst))
OBJS_SYM	:= $(patsubst %, %, $(CFILENDIR:.c=.sym))


VPATH		:= $(SRCDIRS)

.PHONY: clean


#-------------------------------------------------------------------------------
#	start compiling
#-------------------------------------------------------------------------------
$(target).ihx : $(OBJS_REL)
	@echo "linking output files to ihx file..."
	@$(CC) -m$(archive) $(CC_CONFIG) $(LD_CONFIG) $(LIBS) $(OBJS_REL) -o $(target).ihx

	@echo "ihx file to hex file..."
	@packihx $(target).ihx > $(target).hex

	@echo "ihx file to bin file..."
	@makebin -p $(target).ihx > $(target).bin

	@echo "all files build Completed!"


$(OBJS_REL) : %.rel : %.c
	@echo "compiling" $<
	@$(CC) -m$(archive) $(CC_CONFIG) $(INCLUDE) -c -o $@ $<
	

#-------------------------------------------------------------------------------
#	stcgal flash to mcu
#-------------------------------------------------------------------------------
flash:
	@stcgal -p $(serial) $(target).bin $(eeprom_image)
	
erase:
	@stcgal -p $(serial) -e

info:
	@stcgal -p $(serial)


#-------------------------------------------------------------------------------
#	memory info
#-------------------------------------------------------------------------------
mem_info:
	@cat $(target).mem


#-------------------------------------------------------------------------------
#	search serial port device
#-------------------------------------------------------------------------------
search_serial:
	@pyserial-ports


#-------------------------------------------------------------------------------
#	clean object files
#-------------------------------------------------------------------------------
clean:
	@rm $(target).ihx $(target).hex $(target).bin \
	    $(target).lk $(target).map $(target).mem \
		*.rel *.asm *.lst *.rst *.sym